package org.bonorsoft.modules.control.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.control.entity.*;
import org.bonorsoft.modules.control.mapper.AddressMapper;
import org.bonorsoft.modules.control.service.*;
import org.bonorsoft.modules.control.vo.AddressVo;
import org.bonorsoft.modules.control.vo.PopulationVo;
import org.bonorsoft.modules.control.vo.QrcodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 走访地址
 * @Author: WilliamYe
 * @Date:   2021-01-30
 * @Version: V1.0
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Autowired
    private IBuildingService buildingService;

    @Autowired
    private IResidentialService residentialService;

    @Autowired
    private ICommonService commonService;

    @Autowired
    private IHouseService houseService;

    @Autowired
    private IPopulationService populationService;

    /**
     * 查出具有是否包含模板的list
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<AddressVo> paging(Page<Address> page, QueryWrapper<Address> queryWrapper) {
        return this.baseMapper.paging(page, queryWrapper);
    }

    /**
     * 信息保存
     * @param dzbm
     * @param qrcodeStr
     * @param link
     */
    @Override
    public int insertQrcode(String dzbm, String qrcodeStr, String link) {
        return this.baseMapper.insertQrcode(dzbm, qrcodeStr, link);
    }

    @Override
    public int countQrcode(String dzbm) {
        return this.baseMapper.countQrcode(dzbm);
    }

    @Override
    public IPage<DoorCode> pageCode(Page<Address> page, QueryWrapper<Address> queryWrapper) {
        return this.baseMapper.pageCode(page, queryWrapper);
    }

    @Override
    public void setCommonInfoByAdress(House house) {
        Address address = this.getOne(new QueryWrapper<Address>().eq("dzmc", house.getDzmc()));
        String type = address.getDzlx();
        if (oConvertUtils.isNotEmpty(type)){
            if ("JZW".equals(type)){
                Building building = buildingService.getById(address.getDzbm());
                house.setDzbm(building.getDzbm())
                        .setDjdwGajgjgdm(building.getDjdwGajgjgdm())
                        .setDjdwGajgmc(building.getDjdwGajgmc())
                        .setDjrZh(building.getDjrZh())
                        .setDjsj(building.getDjsj())
                        .setSjly(building.getSjly())
                        .setSssqjwwgDzbm(building.getSssqjwwgDzbm())
                        .setSssqjcwgDzbm(building.getSssqjcwgDzbm())
                        .setSsjlxxqDzbm(building.getSsjlxxqDzbm())
                        .setZxdhzb(building.getZxdhzb())
                        .setZxdzzb(building.getZxdzzb())
                        .setInTime(new Date());
            }
            if ("FW".equals(type)){
                Residential residential = residentialService.getById(address.getDzbm());
                house.setDzbm(residential.getDzbm())
                        .setDjdwGajgjgdm(residential.getDjdwGajgjgdm())
                        .setDjdwGajgmc(residential.getDjdwGajgmc())
                        .setDjrZh(residential.getDjrZh())
                        .setDjsj(residential.getDjsj())
                        .setSjly(residential.getSjly())
                        .setSssqjwwgDzbm(residential.getSssqjwwgDzbm())
                        .setSssqjcwgDzbm(residential.getSssqjcwgDzbm())
                        .setZxdhzb(residential.getZxdhzb())
                        .setZxdzzb(residential.getZxdzzb())
                        .setInTime(new Date());
            }
        }
        house.setSyxs(commonService.queryValueByParam(house.getSyxs(), "SQJW_SYXS"))
                .setFwytdm(commonService.queryValueByParam(house.getFwytdm(), "SQJW_FWYT"))
                .setFwlbdm(commonService.queryValueByParam(house.getFwlbdm(), "SQJW_FWLB"))
                .setFwcqxzzldm(commonService.queryValueByParam(house.getFwcqxzzldm(), "SQJW_FWXZ"))
                .setTgrYfzgxRygxdm(commonService.queryValueByParam(house.getTgrYfzgxRygxdm(),"T_PEO_RELATION"));
    }

    @Override
    public void getCommonInfoByAdress(House house) {
        house.setSyxs(getValue(house.getSyxs(), "SQJW_SYXS"))
                .setFwytdm(getValue(house.getFwytdm(), "SQJW_FWYT"))
                .setFwlbdm(getValue(house.getFwlbdm(), "SQJW_FWLB"))
                .setFwcqxzzldm(getValue(house.getFwcqxzzldm(), "SQJW_FWXZ"))
                .setTgrYfzgxRygxdm(getValue(house.getTgrYfzgxRygxdm(),"T_PEO_RELATION"));
    }

    @Override
    public void setUnitByAdress(Unit unit) {
        Address address = this.getOne(new QueryWrapper<Address>().eq("dzmc", unit.getDwmc()));
        String type = address.getDzlx();
        if (oConvertUtils.isNotEmpty(type)){
            if ("JZW".equals(type)){
                Building building = buildingService.getById(address.getDzbm());
                unit.setDjdwGajgjgdm(building.getDzbm())
                        .setDjdwGajgjgdm(building.getDjdwGajgjgdm())
                        .setDjdwGajgmc(building.getDjdwGajgmc())
                        .setDjrZh(building.getDjrZh())
                        .setDjsj(building.getDjsj())
                        .setSjly(building.getSjly())
                        .setSssqjwwgDzbm(building.getSssqjwwgDzbm())
                        .setSssqjcwgDzbm(building.getSssqjcwgDzbm())
                        .setSsjlxxqDzbm(building.getSsjlxxqDzbm())
                        .setZxdhzb(building.getZxdhzb())
                        .setZxdzzb(building.getZxdzzb())
                        .setInTime(new Date());
            }
            if ("FW".equals(type)){
                Residential residential = residentialService.getById(address.getDzbm());
                unit.setDjdwGajgjgdm(residential.getDzbm())
                        .setDjdwGajgjgdm(residential.getDjdwGajgjgdm())
                        .setDjdwGajgmc(residential.getDjdwGajgmc())
                        .setDjrZh(residential.getDjrZh())
                        .setDjsj(residential.getDjsj())
                        .setSjly(residential.getSjly())
                        .setSssqjwwgDzbm(residential.getSssqjwwgDzbm())
                        .setSssqjcwgDzbm(residential.getSssqjcwgDzbm())
                        .setZxdhzb(residential.getZxdhzb())
                        .setZxdzzb(residential.getZxdzzb())
                        .setInTime(new Date());
            }
        }
        unit.setGaglDwfl(commonService.queryValueByParam(unit.getGaglDwfl(), "SQJW_FWSSZT"))
                .setDwlxdm(commonService.queryValueByParam(unit.getDwlxdm(), "SQJW_DWLX_2"))
                .setJjlxdm(commonService.queryValueByParam(unit.getJjlxdm(), "SQJW_JJLX"))
                .setHylbdm(commonService.queryValueByParam(unit.getHylbdm(), "SWJG_DWSSHY"))
                .setJxcsfl(commonService.queryValueByParam(unit.getJxcsfl(), "SQJW_JXCSFL"))
                .setHlbz(commonService.queryValueByParam(unit.getHlbz(), "YBSS_HLBZ"))
                .setJyzt(commonService.queryValueByParam(unit.getJyzt(),"SQJW_JYZT"));

    }

    @Override
    public void getUnitByAdress(Unit unit) {
        unit.setGaglDwfl(getValue(unit.getGaglDwfl(), "SQJW_FWSSZT"))
                .setDwlxdm(getValue(unit.getDwlxdm(), "SQJW_DWLX_2"))
                .setJjlxdm(getValue(unit.getJjlxdm(), "SQJW_JJLX"))
                .setHylbdm(getValue(unit.getHylbdm(), "SWJG_DWSSHY"))
                .setJxcsfl(getValue(unit.getJxcsfl(), "SQJW_JXCSFL"))
                .setHlbz(getValue(unit.getHlbz(), "YBSS_HLBZ"))
                .setJyzt(getValue(unit.getJyzt(),"SQJW_JYZT"));
    }

    @Override
    public void setPopulationByHouse(PopulationVo populationVo) {
        House house = houseService.getOne(new QueryWrapper<House>().eq("dzmc", populationVo.getDzmc()));
        String rkbm = UUID.randomUUID().toString().replaceAll("-", "");
        populationVo.setInTime(new Date())
                .setRkbm(rkbm)
                .setDzbm(house.getDzbm())
                .setDjdwGajgjgdm(house.getDjdwGajgjgdm())
                .setSsjlxxqDzbm(house.getSsjlxxqDzbm())
                .setSssj(house.getSssj())
                .setSsfxj(house.getSsfxj())
                .setSspcs(house.getSspcs())
                .setSszrq(house.getSszrq())
                .setZxdhzb(house.getZxdhzb())
                .setZxdzzb(house.getZxdzzb())
                .setSssqjcwgDzbm(house.getSssqjcwgDzbm())
                .setSssqjwwgDzbm(house.getSssqjwwgDzbm())
                .setSyrkdzzdjlxdm(commonService.queryValueByParam(populationVo.getSyrkdzzdjlxdm(), "SQJW_RY_DZZDJLX"))
                .setSyrkgllbdm(commonService.queryValueByParam(populationVo.getSyrkgllbdm(), "SQJW_SYRKGLLB"))
                .setRhflbz(commonService.queryValueByParam(populationVo.getRhflbz(), "SQJW_RHFLBS"))
                .setSjly(commonService.queryValueByParam(populationVo.getSjly(), "YBSS_SJLY"))
                .setJwrylx(commonService.queryValueByParam(populationVo.getJwrylx(), "YBSS_JWRYLX"))
                .setSyrkhsjg(commonService.queryValueByParam(populationVo.getSyrkhsjg(), "SQJW_RKHSJG"));
    }

    @Override
    @Transactional
    public void addPopuAndPhoto(List<PopulationVo> list) {
        List<Population> populationList = JSONArray.parseArray(JSON.toJSONString(list), Population.class);
        populationService.saveBatchPhoto(list);
        populationService.saveBatch(populationList);
    }

    @Override
    public void getPopulationByHouse(PopulationVo populationVo) {
        populationVo.setSyrkdzzdjlxdm(getValue(populationVo.getSyrkdzzdjlxdm(), "SQJW_RY_DZZDJLX"))
                .setSyrkgllbdm(getValue(populationVo.getSyrkgllbdm(), "SQJW_SYRKGLLB"))
                .setRhflbz(getValue(populationVo.getRhflbz(), "SQJW_RHFLBS"))
                .setSjly(getValue(populationVo.getSjly(), "YBSS_SJLY"))
                .setJwrylx(getValue(populationVo.getJwrylx(), "YBSS_JWRYLX"))
                .setSyrkhsjg(getValue(populationVo.getSyrkhsjg(), "SQJW_RKHSJG"));
    }

    @Override
    public String getQrcode(String dzbm) {
        return this.baseMapper.getQrcode(dzbm);
    }

    @Override
    public List<QrcodeVo> getAllQrcode() {
        return this.baseMapper.getAllQrocde();
    }

    private String getValue(String value, String param){
        return commonService.queryCodeByParam("ep_sys_param_value", "param_value", "param_code", value ,param);
    }


}
